/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpriteFrame.java
 *
 * Created on Apr 14, 2010, 7:11:25 PM
 */

package thetaeditor;
import java.util.LinkedList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.util.Enumeration;

/**
 *
 * @author garrett
 */
public class SpriteFrame extends JFrame {

    private LinkedList<SeqPanel> seqPanels = new LinkedList<SeqPanel>();
    private Sprite editing;

    public SpriteFrame() {
        this(null);
    }

    /** Creates new form SpriteFrame */
    public SpriteFrame(Sprite editing) {
        initComponents();

        if (editing == null) {
            editing = new Sprite();
        } else {
            Enumeration<String> keys = editing.sequences.keys();
            while (keys.hasMoreElements()) {
                String k = keys.nextElement();
                System.out.println("Loading Sequence " + k);
                SeqPanel sp = new SeqPanel(
                        defaultSequenceGroup, editing.sequences.get(k));
                if (k.equals(editing.defaultSequence))
                    sp.setDefault(true);
                seqPanels.add(sp);
            }
            nameField.setText(editing.name);
        }
        this.editing = editing;

        if (seqPanels.size() == 0)
            addSequence(null);

        sequencesPanel.setLayout(new GridBagLayout());

        Listener l = new Listener();
        
        addSeqButton.addActionListener(l);
        removeSeqButton.addActionListener(l);
        okayButton.addActionListener(l);
        cancelButton.addActionListener(l);

        layoutSequences();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        defaultSequenceGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        sequencesPanel = new javax.swing.JPanel();
        okayButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        addSeqButton = new javax.swing.JButton();
        removeSeqButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sprite");

        jLabel1.setText("Name");

        nameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout sequencesPanelLayout = new javax.swing.GroupLayout(sequencesPanel);
        sequencesPanel.setLayout(sequencesPanelLayout);
        sequencesPanelLayout.setHorizontalGroup(
            sequencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 372, Short.MAX_VALUE)
        );
        sequencesPanelLayout.setVerticalGroup(
            sequencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 215, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(sequencesPanel);

        okayButton.setText("Okay");

        cancelButton.setText("Cancel");

        addSeqButton.setText("Add Sequence");

        removeSeqButton.setText("Remove Sequence");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okayButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addSeqButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeSeqButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addSeqButton)
                    .addComponent(removeSeqButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okayButton)
                    .addComponent(cancelButton)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameFieldActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SpriteFrame sf = new SpriteFrame();
                sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                sf.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addSeqButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.ButtonGroup defaultSequenceGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nameField;
    private javax.swing.JButton okayButton;
    private javax.swing.JButton removeSeqButton;
    private javax.swing.JPanel sequencesPanel;
    // End of variables declaration//GEN-END:variables

    public Sprite getSprite() {
        Sprite sprite = new Sprite();
        sprite.name = nameField.getText();
        for (SeqPanel sp : seqPanels) {
            if (sp.isSequenceDefault())
                sprite.defaultSequence = sp.getSequenceName();
            sprite.sequences.put(sp.getSequenceName(), sp.getSequence());
        }
        return sprite;
    }

    private void layoutSequences() {
        sequencesPanel.removeAll();
        for (int i = 0; i < seqPanels.size(); i++) {
            SeqPanel sp = seqPanels.get(i);
            GridBagConstraints gc = new GridBagConstraints();
            gc.gridx = 0;
            gc.gridy = i;
            gc.gridwidth = 1;
            gc.gridheight = 1;
            gc.insets = new Insets(0, 0, 0, 0);
            gc.ipadx = gc.ipadx= 0;
            sequencesPanel.add(sp, gc);
        }
        sequencesPanel.setVisible(false);
        sequencesPanel.setVisible(true);
    }

    private void addSequence(Sequence seq) {
        if (seq == null) seq = new Sequence();
        SeqPanel sp = new SeqPanel(defaultSequenceGroup, seq);
        seqPanels.add(sp);
        layoutSequences();
    }


    private SpriteFrame getSelf() {
        return this;
    }
    class Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == addSeqButton) {
                addSequence(null);
            } else if (e.getSource() == removeSeqButton) {
                int total = 0;
                for (SeqPanel sp : seqPanels)
                    if (sp.isSelected())
                        total++;
                if (total == 0) return;
                if (total > 1) {
                    int confirm = JOptionPane.showConfirmDialog(getSelf(),
                            "Delete " + total + " frames?");
                    if (confirm != JOptionPane.YES_OPTION) return;
                }
                for (int i = 0; i < seqPanels.size(); i++) {
                    if (seqPanels.get(i).isSelected()) {
                        seqPanels.remove(i);
                        i--; // order may have changed
                    }
                }
                layoutSequences();
            } else if (e.getSource() == okayButton) {
                editing.read(getSprite().store());
                setVisible(false);
                dispose();
            } else if (e.getSource() == cancelButton) {
                setVisible(false);
                dispose();
            }
        }
    }
}
